<?php
include('includes/connection_settings.php');

if (!isset($_COOKIE['username'])) {
 echo("Log in first!");
 exit;
}

//Strips " and ' marks
$bannedMarks = array("\"", "'");

$_GET['local'] = str_replace($bannedMarks, "", $_GET['local']);
$_GET['foreign'] = str_replace($bannedMarks, "", $_GET['foreign']);
$_COOKIE['username'] = str_replace($bannedMarks, "", $_COOKIE['username']);
$_GET['dictionary'] = str_replace($bannedMarks, "", $_GET['dictionary']);

//Adds the dictionary to the choices table if neccessery
$table = mysql_query("SELECT dictionary FROM words WHERE dictionary = '{$_GET['dictionary']}'");

//If dictionary is newbie aka it's neccessery
if (@mysql_result($table, 0, 0) == "") {
 if (!mysql_query("INSERT INTO choices (dictionary) VALUES ('{$_GET['dictionary']}')")) {
  echo "<h1>Can't add new data to the table!</h1><p>" . mysql_error($connection) . "</p>";
  exit;
 }
}

//Adds the word
if (!mysql_query("INSERT INTO words (local, theForeign, creator, dictionary) VALUES ('{$_GET['local']}', '{$_GET['foreign']}', '{$_COOKIE['username']}', '{$_GET['dictionary']}')")) {
 echo "<h1>Can't add new data to the table!</h1><p>" . mysql_error($connection) . "</p>";
 exit;
}

mysql_close($connection);

echo "true";

?>
